<?php
///usr/local/php/bin/php -c /usr/local/php/etc/php.ini /data/www/esmj_script/script.php request/TaskHandler

/**
 * 任务写入DB
 */
class TaskHandler extends ScriptBase
{
    public function logic()
    {
        $mysql = $this->mysql('default');
        //set_time_limit(0);
        //while (true)
       // {
            $redis = $this->redis('status');
            $time = date('Ymd',strtotime('-1 days'));
            $taskKey = 'TO:*_'.$time;
            $redisKeys = $redis->keys($taskKey);
            if(count($redisKeys) >= 1){
                foreach( $redisKeys as $v ){
                    $pushData = $redis->HMget($v, ['uid','is_push_db','num','last_time']);
                    if( $pushData['is_push_db'] || !$pushData['uid']){
                        continue;
                    }
                    $sql = "select uid from task_open where task_time = {$time} and uid = ".$pushData['uid'];
                    $res = $mysql->query( $sql );
                    if(!$res){
                        $insertData = [
                            'uid' => $pushData['uid'],
                            'task_time' => $time,
                            'open_time' => $pushData['last_time'],
                            'open_num' => $pushData['num'],
                            'last_open_time' => $pushData['last_time'],
                        ];
                        $mysql->insert( 'task_open', $insertData );
                    }else{
                        $insertData = [
                            'last_open_time' => $pushData['last_time'],
                            'open_num' => $pushData['num'],
                        ];
                        $where = [
                            'uid' => $pushData['uid'],
                            'task_time' => $time,
                        ];
                        $mysql->update( 'task_open', $insertData ,$where );
                    }
                    $redis->Hset($v, 'is_push_db', 1);
                }
            }
           // sleep(5);
         //$this->initRedis('status');
       // }
    }
}